﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Mail;
using CivilRegistry.Logger;
using CivilRegistry.ControlledModel.Extensions;

namespace CivilRegistry.Networking
{
    /// <summary>
    /// An object to send emails.
    /// </summary>
    public class EmailEngine
    {
        string from;

        /// <summary>
        /// Gets or Sets the From address.
        /// </summary>
        public string From
        {
            get { return from; }
            set { from = value; }
        }
        List<string> to;

        /// <summary>
        /// Gets the To address list.
        /// </summary>
        public List<string> To
        {
            get { return to; }
        }
        List<string> cc;

        /// <summary>
        /// Gets the Cc address list.
        /// </summary>
        public List<string> Cc
        {
            get { return cc; }
        }
        List<string> bcc;

        /// <summary>
        /// Gets the Bcc address list.
        /// </summary>
        public List<string> Bcc
        {
            get { return bcc; }
        }
        string subject;

        /// <summary>
        /// Gets or Sets the Subject of the email.
        /// </summary>
        public string Subject
        {
            get { return subject; }
            set { subject = value; }
        }
        string body;

        /// <summary>
        /// Gets or Sets the Body of the email.
        /// </summary>
        public string Body
        {
            get { return body; }
            set { body = value; }
        }

        /// <summary>
        /// Initialises the properties with default values.
        /// </summary>
        public EmailEngine()
        {
            to = new List<string>();
            bcc = new List<string>();
            cc = new List<string>();
        }

        /// <summary>
        /// Method to Send emails.
        /// </summary>
        /// <returns>Returns True if succeeded, else false.</returns>
        public bool Send()
        {
            return this.Send(this.to, this.cc, this.bcc, this.subject, this.body);
        }

        /// <summary>
        /// Method to Send emails.
        /// </summary>
        /// <param name="to">The to address list.</param>
        /// <param name="subject">The subject of the email.</param>
        /// <param name="body">The body text of the email.</param>
        /// <returns>Returns True if succeeded, else false.</returns>
        public bool Send(List<string> to, string subject, string body)
        {
            return this.Send(to, this.cc, this.bcc, subject, body);
        }

        /// <summary>
        /// Method to Send emails.
        /// </summary>
        /// <param name="to">The to address list.</param>
        /// <param name="cc">The carbon copy address list.</param>
        /// <param name="bcc">The blind carbon copy address list.</param>
        /// <param name="subject">The subject of the email.</param>
        /// <param name="body">The body text of the email.</param>
        /// <returns>Returns True if succeeded, else false.</returns>
        public bool Send(List<string> to, List<string> cc, List<string> bcc, string subject, string body)
        {
            to.Required("To");
            subject.Required("Subject");
            body.Required("Body");

            try
            {
                MailMessage message = new MailMessage();

                if (!String.IsNullOrEmpty(from))
                    message.From = new MailAddress(from);

                to.ForEach(address => message.To.Add(address));
                cc.ForEach(address => message.CC.Add(address));
                bcc.ForEach(address => message.Bcc.Add(address));

                message.Subject = subject;
                message.Body = body;
                message.IsBodyHtml = true;

                SmtpClient sender = new SmtpClient();

                sender.Send(message);

                return true;
            }
            catch (Exception ex)
            {
                ErrorLogger.Log(ex);
                return false;
            }
        }

    }
}